package edu.hawaii.senin.iclouds.simple;

import java.util.Iterator;
import java.util.TreeMap;

import edu.hawaii.senin.iclouds.basics.Reaction;

/**
 * Encapsulates reactions.
 * 
 * @author Pavel Senin.
 * 
 */
public class CloudReactions implements Iterable<Reaction> {

  /**
   * Storage.
   */
  private TreeMap<String, Reaction> storage = new TreeMap<String, Reaction>();

  /**
   * Adds the new reaction into the storage.
   * 
   * @param r new reaction to add.
   * @throws CloudReactionsException if trying to add reaction that already in.
   */
  public void add(Reaction r) throws CloudReactionsException {
    if (this.storage.containsKey(r.getName())) {
      throw new CloudReactionsException("population already contains reaction" + r.toArray());
    }
    else {
      this.storage.put(r.getName(), r);
    }
  }

  /**
   * Provides iterator over the stored reactions.
   * 
   * @return Iterator over the collection.
   */
  public Iterator<Reaction> iterator() {
    return this.storage.values().iterator();
  }

  /**
   * Reports this reactions collection size.
   * 
   * @return reactions collection size.
   */
  public Integer size() {
    return this.storage.size();
  }

  /**
   * Gets specified reaction from the storage.
   * 
   * @param key reaction ID.
   * @return specified reaction or null value.
   */
  public Reaction get(String key) {
    return this.storage.get(key);
  }

}
